#include <Windows.h>
#include <tchar.h>
#include "resource.h"

int CALLBACK DlgProc(HWND hDlg, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
	static HMODULE hModule	= NULL;
	static HICON hIcon		= NULL;
	static HBITMAP hBmp		= NULL;

	switch (nMsg)
	{
		case WM_INITDIALOG:
		{
			SetWindowText(hDlg, _T("Demo0122"));

			UINT nWidth		= GetSystemMetrics(SM_CXSCREEN);
			UINT nHeight	= GetSystemMetrics(SM_CYSCREEN);
			RECT rtClient;
			GetClientRect(hDlg, &rtClient);
			SetWindowPos(hDlg, NULL, (nWidth - (rtClient.right - rtClient.left)) / 2, (nHeight - (rtClient.bottom - rtClient.top)) / 2, 0, 0, SWP_NOSIZE);
			
			break;
		}
		case WM_COMMAND:
		{
			switch (LOWORD(wParam))
			{
				case IDC_BTNRETRIEVE:
				{
					hModule = LoadLibrary(_T("Demo0122Ex.exe"));
					if (NULL != hModule)
					{
						hIcon	= LoadIcon(hModule, MAKEINTRESOURCE(103));
						hBmp	= LoadBitmap(hModule, MAKEINTRESOURCE(104));
						if (NULL != hIcon)
						{
							SendMessage(GetDlgItem(hDlg, IDC_STCICON), STM_SETIMAGE, (WPARAM)IMAGE_ICON, (LPARAM)hIcon);
						}
						if (NULL != hBmp)
						{
							SendMessage(GetDlgItem(hDlg, IDC_STCBMP), STM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hBmp);
						}
					}
					break;
				}
			}
			break;
		}
		case WM_CLOSE:
		{
			if (NULL != hIcon)
			{
				DestroyIcon(hIcon);
			}
			if (NULL != hBmp)
			{
				DeleteObject(hBmp);
			}

			if (NULL != hModule)
			{
				FreeLibrary(hModule);
			}
			PostQuitMessage(0);
			break;
		}
	}

	return 0;
}

BOOL WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPTSTR szCmdLine, int nShowCmd)
{
	DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG), NULL, DlgProc);

	return TRUE;
}